package com.linln.admin.config;

import com.linln.common.utils.ResultVoUtil;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

@Aspect
@Component
public class PcsInterceptor {

    @Value("${securitys.hgKey}")
    private  String SECRET_HG_KEY;

    @Value("${securitys.slabKey}")
    private  String SECRET_SLAB_KEY;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Pointcut("execution(* com.linln.admin.pcs.api.*.*(..))")
    public void controllerMethods() {}

    // 使用 @Around 拦截方法
    @Around("controllerMethods()")
    public Object interceptControllerMethods(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取当前的 HttpServletRequest 对象
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String uri = request.getRequestURI();
        if (!uri.contains("login")){
            if (uri.contains("/api/patient")||uri.contains("/api/wx")||uri.contains("/api/js/wechat") ){
                return joinPoint.proceed();
            }
            Enumeration<String> headerNames = request.getHeaderNames();
            int flag = 1;
            for (Enumeration<String> e = headerNames; e.hasMoreElements();){
                String headerName = e.nextElement();
                if ("Authorization".equals(headerName)||"authorization".equals(headerName)){
                    flag = 2;
                    break;
                }
            }
            if (flag == 2){
                // 获取请求头参数
                String headerValue = request.getHeader("Authorization");
                if (StringUtils.isNotEmpty(headerValue)){
                    if (headerValue.contains("Bearer")){
                        String[] s = headerValue.split("Bearer");
                        if (s.length >1){
                            String token = s[1].trim();
                            if (uri.contains("/api/porter")){
                                //校验Token是否正确
                                Boolean bool = jwtTokenUtil.validateToken(token, SECRET_HG_KEY);
                                if (bool){
                                    return joinPoint.proceed();
                                }
                            } else if (uri.contains("/api/slab")) {
                                //校验Token是否正确
                                Boolean bool = jwtTokenUtil.validateToken(token,SECRET_SLAB_KEY);
                                if (bool){
                                    return joinPoint.proceed();
                                }
                            }
                        }else{
                            return joinPoint.proceed();
                        }
                    }
                }
            }
           // return joinPoint.proceed();
            return ResultVoUtil.error(601,"未登录");
        }else{
            return joinPoint.proceed();
        }
    }
}
